package com.akeil.flash.dialog;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.akeil.flash.config.EDeviceType;
import com.akeil.flash.config.IConstantsConfig;
import com.akeil.flash.pojo.Other;
import com.akeil.flash.pojo.Parameter;
import com.akeil.flash.preferences.IPreferenceConstants;
import com.akeil.flash.service.IParameterService;
import com.akeil.flash.service.ParameterServiceImpl;

import flash.Activator;
import flash.Application;

public class BaseSettingDialog extends TitleAreaDialog {
	IParameterService parameterService = Application.applicationContext.getBean(ParameterServiceImpl.class);
	IPreferenceStore store = Activator.getDefault().getPreferenceStore();
	private DecimalFormat df2 = new DecimalFormat("0.00");
	
	private Text textSampleName;
	private Text textSampleNumber;
	private Text textPreSettingTemperature;
	private Text textAfterTemperature;
	
	private String sampleName;
	private String sampleNumber;
	private String preSettingTemperature;
	private String afterTemperature;
	private String testStep;
	
	List<Parameter> parameters = new ArrayList<Parameter>();
	
	public BaseSettingDialog(Shell parentShell) {
		super(parentShell);
		// TODO Auto-generated constructor stub
	}

	protected Control createDialogArea(Composite parent) {
		Composite composite = (Composite) super.createDialogArea(parent);
		initData();
		
		Composite composite_1 = new Composite(composite, SWT.NONE);
		composite_1.setLayout(new FormLayout());
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		Label lblNewLabel = new Label(composite_1, SWT.NONE);
		FormData fd_lblNewLabel = new FormData();
		fd_lblNewLabel.top = new FormAttachment(0, 10);
		fd_lblNewLabel.left = new FormAttachment(0, 10);
		lblNewLabel.setLayoutData(fd_lblNewLabel);
		lblNewLabel.setText("样品名称");
		
		Label lblNewLabel_1 = new Label(composite_1, SWT.NONE);
		FormData fd_lblNewLabel_1 = new FormData();
		fd_lblNewLabel_1.top = new FormAttachment(lblNewLabel, 14);
		fd_lblNewLabel_1.left = new FormAttachment(0, 10);
		lblNewLabel_1.setLayoutData(fd_lblNewLabel_1);
		lblNewLabel_1.setText("样品编号");
		
		Label label = new Label(composite_1, SWT.NONE);
		FormData fd_label = new FormData();
		fd_label.top = new FormAttachment(lblNewLabel_1, 14);
		fd_label.left = new FormAttachment(0, 10);
		label.setLayoutData(fd_label);
		label.setText("预置温度");
		
		Label lblNewLabel_2 = new Label(composite_1, SWT.NONE);
		FormData fd_lblNewLabel_2 = new FormData();
		fd_lblNewLabel_2.top = new FormAttachment(label, 14);
		fd_lblNewLabel_2.left = new FormAttachment(0, 10);
		lblNewLabel_2.setLayoutData(fd_lblNewLabel_2);
		lblNewLabel_2.setText("滞后温度");
		
		Label label_1 = new Label(composite_1, SWT.NONE);
		label_1.setText("试验步骤");
		FormData fd_label_1 = new FormData();
		fd_label_1.left = new FormAttachment(lblNewLabel, 0, SWT.LEFT);
		label_1.setLayoutData(fd_label_1);
		
		if(IConstantsConfig.deviceType == EDeviceType.MANUAL){
			label_1.setVisible(true);
		}else{
			label_1.setVisible(false);
		}
		
		
		textSampleName = new Text(composite_1, SWT.BORDER);
		textSampleName.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				sampleName = textSampleName.getText();
				parameters.get(0).setSamplename(sampleName);
			}
		});
		FormData fd_textSampleName = new FormData();
		fd_textSampleName.right = new FormAttachment(lblNewLabel, 334, SWT.RIGHT);
		fd_textSampleName.top = new FormAttachment(0, 10);
		fd_textSampleName.left = new FormAttachment(lblNewLabel, 34);
		textSampleName.setLayoutData(fd_textSampleName);
		textSampleName.setText(parameters.get(0).getSamplename());
		
		textSampleNumber = new Text(composite_1, SWT.BORDER);
		textSampleNumber.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				sampleNumber = textSampleNumber.getText();
				parameters.get(0).setSamplenumber(sampleNumber);
			}
		});
		FormData fd_textSampleNumber = new FormData();
		fd_textSampleNumber.right = new FormAttachment(textSampleName, 300);
		fd_textSampleNumber.top = new FormAttachment(lblNewLabel_1, 0, SWT.TOP);
		fd_textSampleNumber.left = new FormAttachment(textSampleName, 0, SWT.LEFT);
		textSampleNumber.setLayoutData(fd_textSampleNumber);
		textSampleNumber.setText(parameters.get(0).getSamplenumber());
		
		textPreSettingTemperature = new Text(composite_1, SWT.BORDER);
		textPreSettingTemperature.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				preSettingTemperature = textPreSettingTemperature.getText();
				parameters.get(0).setPretemperature(Double.parseDouble(preSettingTemperature));
			}
		});
		FormData fd_textPreSettingTemperature = new FormData();
		fd_textPreSettingTemperature.right = new FormAttachment(textSampleNumber, 300);
		fd_textPreSettingTemperature.left = new FormAttachment(label, 34);
		fd_textPreSettingTemperature.top = new FormAttachment(label, 0, SWT.TOP);
		textPreSettingTemperature.setLayoutData(fd_textPreSettingTemperature);
		textPreSettingTemperature.setText(df2.format(parameters.get(0).getPretemperature()));
		
		textAfterTemperature = new Text(composite_1, SWT.BORDER);
		textAfterTemperature.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				afterTemperature = textAfterTemperature.getText();
				parameters.get(0).setNexttemperature(Double.parseDouble(afterTemperature));
			}
		});
		FormData fd_textAfterTemperature = new FormData();
		fd_textAfterTemperature.right = new FormAttachment(textPreSettingTemperature, 300);
		fd_textAfterTemperature.top = new FormAttachment(lblNewLabel_2, 0, SWT.TOP);
		fd_textAfterTemperature.left = new FormAttachment(textSampleName, 0, SWT.LEFT);
		textAfterTemperature.setLayoutData(fd_textAfterTemperature);
		textAfterTemperature.setText(df2.format(parameters.get(0).getNexttemperature()));
		
		Label label_4 = new Label(composite_1, SWT.NONE);
		FormData fd_label_4 = new FormData();
		fd_label_4.top = new FormAttachment(0, 75);
		fd_label_4.left = new FormAttachment(textPreSettingTemperature, 28);
		label_4.setLayoutData(fd_label_4);
		
		if(IConstantsConfig.deviceType == EDeviceType.MANUAL){
			label_4.setText("（0-300℃）");
		}else{
			label_4.setText("（0-400℃）");
		}		
		
		Label label_3 = new Label(composite_1, SWT.NONE);
		label_3.setText("（0-100℃）");
		FormData fd_label_3 = new FormData();
		fd_label_3.top = new FormAttachment(label_4, 14);
		fd_label_3.left = new FormAttachment(textAfterTemperature, 28);
		label_3.setLayoutData(fd_label_3);
		
		testStep = parameters.get(0).getMethod();
		Combo comboTestStep = new Combo(composite_1, SWT.NONE);
		comboTestStep.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(comboTestStep.getSelectionIndex() == 0){
					testStep = "A";
				}else{
					testStep = "B";
				}
				
				parameters.get(0).setMethod(testStep);
			}
		});
		fd_label_1.top = new FormAttachment(comboTestStep, 3, SWT.TOP);
		FormData fd_comboTestStep = new FormData();
		fd_comboTestStep.top = new FormAttachment(textAfterTemperature, 7);
		fd_comboTestStep.right = new FormAttachment(textSampleName, 300);
		fd_comboTestStep.left = new FormAttachment(textSampleName, 0, SWT.LEFT);
		comboTestStep.setLayoutData(fd_comboTestStep);
		comboTestStep.setItems(new String[]{"A","B"});
		if(testStep.equals("A")){
			comboTestStep.select(0);
		}else{
			comboTestStep.select(1);
		}
		
		
		if(IConstantsConfig.deviceType == EDeviceType.MANUAL){
			comboTestStep.setVisible(true);
		}else{
			comboTestStep.setVisible(false);
		}
		
		this.setTitle("设置参数");
		return composite;
	}
	
	@Override
	protected void okPressed() {
		// TODO 自动生成的方法存根
		saveData();
		super.okPressed();
	}
	
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("基本参数");
	}
	
	@Override
	public void setErrorMessage(String newErrorMessage) {
		// TODO Auto-generated method stub
		Button okButton = getButton(IDialogConstants.OK_ID);
        if (okButton != null) 
            okButton.setEnabled(newErrorMessage == null);
        
		super.setErrorMessage(newErrorMessage);
	}

	public String getSampleName() {
		return sampleName;
	}

	public void setSampleName(String sampleName) {
		this.sampleName = sampleName;
	}

	public String getSampleNumber() {
		return sampleNumber;
	}

	public void setSampleNumber(String sampleNumber) {
		this.sampleNumber = sampleNumber;
	}

	public String getPreSettingTemperature() {
		return preSettingTemperature;
	}

	public void setPreSettingTemperature(String preSettingTemperature) {
		this.preSettingTemperature = preSettingTemperature;
	}

	public String getAfterTemperature() {
		return afterTemperature;
	}

	public void setAfterTemperature(String afterTemperature) {
		this.afterTemperature = afterTemperature;
	}

	public String getTestStep() {
		return testStep;
	}

	public void setTestStep(String testStep) {
		this.testStep = testStep;
	}
	
	private void initData(){
		parameters = parameterService.selectAll();
	}

	
	private void saveData(){
		parameterService.updateByPrimaryKey(parameters.get(0));
	}
}
